import { createStore } from 'redux'


const listReducer = (state = { list: [] }, action) => {
    switch (action.type) {

        case 'add_item':
            // console.log('add_item');
            // console.log(action.payload);
            return { ...state, list: [action.payload, ...state.list] }

        case 'del_item':
            // console.log('del_item');
            // let newList = state.list.filter((item, index) => state.list.key !== action.key)
            let newList = state.list;
            for (let i = 0; i < newList.length; i++) {
                if (newList[i].key === action.key) {
                    newList.splice(i, 1)
                }
            }
            console.log(newList);
            return { ...state, list: [...newList] }

        default:
            return state;
    }
}


const store = createStore(listReducer);
export default store;